package Leetcode;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class problem144 {

    private class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) { val = x; }
    }

    public List<Integer> preorderTraversal(TreeNode root) {

        List<Integer> aList = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();

        if(root==null){
            return aList;
        }

        stack.push(root);
        while(!stack.isEmpty()){
            TreeNode curNode = stack.pop();
            aList.add(curNode.val);
            if(curNode.right!=null){
                stack.push(curNode.right);
            }
            if(curNode.left!=null){
                stack.push(curNode.left);
            }
        }
        return aList;
    }
}
